<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {

	var $table_name = 'a_users';
	var $username = '';
	var $email = '';
	var $password = '';

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * Get all users
	 *
	 * @return	object
	 */
	function get_all_users()
	{
		$this->db->order_by('username','asc');
	
		$query = $this->db->get($this->table_name);
		return $query->result();
	}
	
	/**
	 * Encrypts and salts a password
	 */
	function _prep_password($password)
	{
		return md5($password.$this->config->item('encryption_key'));
	}
	
	/**
	 * Get user record by username and password
	 *
	 * @param	string
	 * @return	object
	 */
	function get_user_by_login($username, $password)
	{
		$this->db->where("(LOWER(username) = '" . strtolower($username) . "' OR LOWER(email) = '" . strtolower($username) . "')");
		$this->db->where('password', $this->_prep_password($password));

		$query = $this->db->get($this->table_name);

		if($query->num_rows() == 1) return $query->row();
		return NULL;
	}
	
	/**
	 * Get user record by username
	 *
	 * @param	string
	 * @return	object
	 */
	function get_user_by_username($username)
	{
		$this->db->where('LOWER(username)', strtolower($username));

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
		return NULL;
	}
	
	/**
	 * Get user record by email
	 *
	 * @param	string
	 * @return	object
	 */
	function get_user_by_email($email)
	{
		$this->db->where('LOWER(email)', strtolower($email));

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
		return NULL;
	}
	
	/**
	 * Get user record by id
	 *
	 * @param	string
	 * @return	object
	 */
	function get_user_by_id($id)
	{
		$this->db->where('id', $id);

		$query = $this->db->get($this->table_name);
		if ($query->num_rows() == 1) return $query->row();
		return NULL;
	}
	
	/**
	 * Check if username available for registering
	 *
	 * @param	string
	 * @return	bool
	 */
	function is_username_available($username)
	{
		$this->db->select('1', FALSE);
		$this->db->where('LOWER(username)', strtolower($username));

		$query = $this->db->get($this->table_name);
		return $query->num_rows() == 0;
	}

	/**
	 * Check if email available for registering
	 *
	 * @param	string
	 * @return	bool
	 */
	function is_email_available($email)
	{
		$this->db->select('1', FALSE);
		$this->db->where('LOWER(email)', strtolower($email));

		$query = $this->db->get($this->table_name);
		return $query->num_rows() == 0;
	}
	
	/**
	 * Create new user record
	 *
	 * @param	array
	 * @param	bool
	 * @return	array
	 */
	function create_user($data)
	{
		if ($this->db->insert($this->table_name, $data)) {
			return $this->db->insert_id();
		}
		return NULL;
	}
	
	/**
	 * Update a users password by their id
	 */
	function update_user_password($id, $password)
	{
		echo $id.' - '.$password;
		$data = array('password' => $this->_prep_password($password));
		$this->db->update($this->table_name, $data, 'id = ' . $id);
	}
	
	/**
	 * Update the LastOnline column for the user
	 *
	 */
	function update_last_online($id)
	{
		$data = array('date_lastonline' => date('Y-m-d H:i:s'));
		$this->db->update($this->table_name, $data, "id = '" . $id . "'");
	}	
 }
 ?>